package js;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import util.Config;
import util.DBUtils;

public class StatEachDayMain {

private static Logger log = LogManager.getLogger(StatEachDayMain.class);

	private DBUtils dbUtils = null;
	private Connection conn = null;
	private String statsDate = null;

	StatEachDayMain(){
		dbUtils = new DBUtils();
		conn = dbUtils.getConn();
	}

	public static void main(String[] args) {

		StatEachDayMain sDayMain = new StatEachDayMain();
		String sz = "�����У�" + 0;
		String nj = "���Ͼ��У�" + 0;
		String wx = "�������У�" + 0;
		String cz = "�������У�" + 0;
		String nt = "����ͨ�У�" + 0;
		String yz = "�������У�" + 0;
		String zj = "�����У�" + 0;
		String tz = "��̩���У�" + 0;
		String xz = "�������У�" + 0;
		String yc = "���γ��У�" + 0;
		String ha = "�������У�" + 0;
		String lyg = "�����Ƹ��У�" + 0;
		String sq = "����Ǩ�У�" + 0+"��";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		sDayMain.statsDate = sdf.format(new Date());
		if(!"".equals(Config.stats_date)){
			sDayMain.statsDate = Config.stats_date;
		}
		log.info("statsDate:" + sDayMain.statsDate);

		List<Map<String, String>> list = sDayMain.getStats();

		StringBuffer content = new StringBuffer();
		String city_id = "";
		String amount = "";
		int total = 0;
		for (Map<String, String> map : list) {
			city_id = map.get("city_id");
			amount = map.get("amount");
			total += Integer.parseInt(amount);
			if("11".equals(city_id)){
				sz = "�����У�" + amount;
			} else if("14".equals(city_id)) {
				nj = "���Ͼ��У�" + amount;
			} else if("12".equals(city_id)) {
				ha = "�������У�" + amount;
			} else if("13".equals(city_id)) {
				sq ="����Ǩ�У�" + amount+"��";
			} else if("15".equals(city_id)) {
				lyg = "�����Ƹۣ�" + amount;
			} else if("16".equals(city_id)) {
				xz ="�������У�" + amount;
			} else if("17".equals(city_id)) {
				cz = "�������У�" + amount;
			} else if("18".equals(city_id)) {
				zj ="�����У�" + amount;
			} else if("19".equals(city_id)) {
				wx = "�������У�" + amount;
			} else if("20".equals(city_id)) {
				nt = "����ͨ�У�" + amount;
			} else if("21".equals(city_id)) {
				tz = "��̩���У�" + amount;
			} else if("22".equals(city_id)) {
				yc ="���γ��У�" + amount;
			} else if("23".equals(city_id)) {
				yz = "�������У�" + amount;
			}
		}
		content.append("��ӭ��Ƭ����������"+total+"������" );
//		��ӭ��Ƭ����������1072217�����������У�522654���Ͼ��У�0�������У�22806�������У�126938����ͨ�У�18093�������У�20947��
//		���У�76433��̩���У�7088�������У�88587���γ��У�11692�������У�65814�����Ƹۣ�73965����Ǩ�У�37200��
		String msg = content.toString()+sz+nj+wx+cz+nt+yz+zj+tz+xz+yc+ha+lyg+sq;
		log.info("��������Ϊ��" + msg);
		sDayMain.insertWarnSmsToBosssmSend(msg);
	}

	private List<Map<String,String>> getStats(){

		PreparedStatement pres = null;
		ResultSet rs = null;
		Map<String, String> map = null;
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		String start_time = statsDate + " 00:00:00";
		String end_time = statsDate + " 23:59:59";
		String sql = "select city_id, sum(1) " +
				"  from sms_report_detail  " +
				" where mgr_id is not null and  send_state = 0 and city_id is not null and city_id!=25" +
				"and send_time > " +
				"to_date('" + start_time + "', 'yyyy/mm/dd hh24:mi:ss') " +
				"and send_time < " +
				"to_date('" + end_time + "', 'yyyy/mm/dd hh24:mi:ss') " +
				" group by city_id " +
				" order by city_id asc" ;
        log.info("sql:" + sql);
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			pres = conn.prepareStatement(sql);
			rs = pres.executeQuery();
			while(rs.next()){
				map = new HashMap<String, String>();
				map.put("city_id", rs.getString(1));
				map.put("amount", rs.getString(2));
				list.add(map);
			}

		} catch (SQLException e) {
			log.error("ͳ�������쳣,���ڣ�"+ statsDate + "������");
			e.printStackTrace();
		} finally{
			close(pres, rs);
		}
		return list;
	}

	/**
	 * ���澯���Ų�����ŷ��ͱ�
	 * @param content
	 */
	private void insertWarnSmsToBosssmSend(String content){

		PreparedStatement pres = null;
		String result = "-1";
		String fetch_id = "";
		String city_id = "25";
		String sql = "insert into boss_sm_send(id,receiver,sender,content,result,create_date,modify_date,fetch_id,city_id)" +
				"values(seq_bomc_cmpp.nextval,?,?,?,?,sysdate,sysdate,?,?)";
		try {
			if(conn == null){
				conn = dbUtils.getConn();
			}
			conn.setAutoCommit(false);
			pres = conn.prepareStatement(sql);
			String[] str = null;
			str = Config.receiver.split(",");
			for (int i = 0; i < str.length; i++) {
				pres.setString(1, str[i]);
				pres.setString(2, Config.sender);
				pres.setString(3, content);
				pres.setString(4, result);
				pres.setString(5, fetch_id);
				pres.setString(6, city_id);
				pres.addBatch();
			}
			pres.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			log.error("����澯���������ŷ��ͱ�����쳣������");
			e.printStackTrace();
		} finally{
			close(pres, null);
		}
	}

	/**
	 * �ر�Ԥ����ͽ�����ͷ���Դ
	 */
	private void close(PreparedStatement pres,ResultSet rs){
		try {
			if(rs!=null){
				rs.close();
			}
			if(pres!=null){
				pres.close();
			}
		} catch (Exception e) {
			log.error("�رս���������쳣" + e.fillInStackTrace());
		}
	}

}
